<?php

class Test_Collection extends WSB_Collection2
{
    
}
class Test_Feuser2 extends WSB_Model2
{

    public function getUsername()
    {
        return 'XXX' . $this->_data['username'] . 'YYY';
    }

}

class Test_Feuser1 extends WSB_Model
{
    protected $_data = array(
        'id' => null,
        'username' => null,
        'password' => null,
        'salutation' => null,
        'firstname' => null,
        'lastname' => null,
        'address' => null,
        'plz' => null,
        'location' => null,
        'country' => null,
        'phone1' => null,
        'phone2' => null,
        'fax' => null,
        'mobile' => null,
        'email' => null,
        'homepage' => null,
        'hidden' => null,
        'deleted' => null,
        'modified' => null,
        'created' => null
    );

}

class Test_Feuser3 extends WSB_Model3
{
    protected $_id;
    protected $_username;
    protected $_password;
    protected $_salutation;
    protected $_firstname;
    protected $_lastname;
    protected $_address;
    protected $_plz;
    protected $_location;
    protected $_country;
    protected $_phone1;
    protected $_phone2;
    protected $_fax;
    protected $_mobile;
    protected $_email;
    protected $_homepage;
    protected $_hidden;
    protected $_deleted;
    protected $_modified;
    protected $_created;

    public function setId($value)
    {
        $this->_id = $value;
    }

    public function getId()
    {
        return $this->_id;
    }

    public function setUsername($value)
    {
        $this->_username = $value;
    }

    public function getUsername()
    {
        return $this->_username;
    }

    public function setPassword($value)
    {
        $this->_password = $value;
    }

    public function getPassword()
    {
        return $this->_password;
    }

    public function setSalutation($value)
    {
        $this->_salutation = $value;
    }

    public function getSalutation()
    {
        return $this->_salutation;
    }

    public function setFirstname($value)
    {
        $this->_firstname = $value;
    }

    public function getFirstname()
    {
        return $this->_firstname;
    }

    public function setLastname($value)
    {
        $this->_lastname = $value;
    }

    public function getLastname()
    {
        return $this->_lastname;
    }

    public function setAddress($value)
    {
        $this->_address = $value;
    }

    public function getAddress()
    {
        return $this->_address;
    }

    public function setPlz($value)
    {
        $this->_plz = $value;
    }

    public function getPlz()
    {
        return $this->_plz;
    }

    public function setLocation($value)
    {
        $this->_location = $value;
    }

    public function getLocation()
    {
        return $this->_location;
    }

    public function setCountry($value)
    {
        $this->_country = $value;
    }

    public function getCountry()
    {
        return $this->_country;
    }

    public function setPhone1($value)
    {
        $this->_phone1 = $value;
    }

    public function getPhone1()
    {
        return $this->_phone1;
    }

    public function setPhone2($value)
    {
        $this->_phone2 = $value;
    }

    public function getPhone2()
    {
        return $this->_phone2;
    }

    public function setFax($value)
    {
        $this->_fax = $value;
    }

    public function getFax()
    {
        return $this->_fax;
    }

    public function setMobile($value)
    {
        $this->_mobile = $value;
    }

    public function getMobile()
    {
        return $this->_mobile;
    }

    public function setEmail($value)
    {
        $this->_email = $value;
    }

    public function getEmail()
    {
        return $this->_email;
    }

    public function setHomepage($value)
    {
        $this->_homepage = $value;
    }

    public function getHomepage()
    {
        return $this->_homepage;
    }

    public function setHidden($value)
    {
        $this->_hidden = $value;
    }

    public function getHidden()
    {
        return $this->_hidden;
    }

    public function setDeleted($value)
    {
        $this->_deleted = $value;
    }

    public function getDeleted()
    {
        return $this->_deleted;
    }

    public function setModified($value)
    {
        $this->_modified = $value;
    }

    public function getModified()
    {
        return $this->_modified;
    }

    public function setCreated($value)
    {
        $this->_created = $value;
    }

    public function getCreated()
    {
        return $this->_created;
    }

}

class Test_Model extends WSB_Model3
{
    protected $_text1;
    protected $_text2;
    protected $_int1;
    protected $_int2;

    public function setText1($value)
    {
        $this->_text1 = trim($value);
    }

    public function getText1()
    {
        return $this->_text1;
    }

    public function setText2($value)
    {
        $this->_text2 = trim($value);
    }

    public function getText2()
    {
        return $this->_text2;
    }

    public function setInt1($value)
    {
        $this->_int1 = intval($value);
    }

    public function getInt1()
    {
        return $this->_int1;
    }

    public function setInt2($value)
    {
        $this->_int2 = intval($value);
    }

    public function getInt2()
    {
        return $this->_int2;
    }

}

class Test_IndexController extends Zend_Controller_Action
{

	public function init()
	{
            parent::init(); // immer zuerst!

            $this->_models = array('Member_Model_DbTable_Begroup', 'Member_Model_DbTable_Fegroup');

            $navigation = $this->_helper->getHelper('Navigation');
            $navigation->add(WSB_ADMIN_URL . 'test/index/', 'Index');
            $navigation->add(WSB_ADMIN_URL . 'test/extjs/', 'EXT-JS');
            $navigation->add(WSB_ADMIN_URL . 'test/yui/', 'YUI');
            $navigation->add(WSB_ADMIN_URL . 'test/dojo/', 'Dojo');

	}


	public function preDispatch()
	{
	    $this->_response->appendBody('<div style="background-color:#f3f3f3;padding:1em;margin-bottom:1.5em;">');
            $this->_response->appendBody('<p style="margin:0"><b>Tests</b></p>');
            $this->_response->appendBody('<ul style="margin:0;padding:0;list-style-type:none;">');
            //$this->_response->appendBody('<li>' . WSB_Controller_Url::getHtmlLink('Model', WSB_Controller_Url::getUrl('index', 'model', null, 'test')) . '</li>');
            $this->_response->appendBody('<li>' . WSB_Controller_Url::getHtmlLink('Editor (basierend auf Zend_Form)', WSB_Controller_Url::getUrl('index', 'editor', null, 'test')) . '</li>');
            $this->_response->appendBody('<li>' . WSB_Controller_Url::getHtmlLink('Paginator', WSB_Controller_Url::getUrl('index', 'paginator', null, 'test')) . '</li>');
            $this->_response->appendBody('<li>' . WSB_Controller_Url::getHtmlLink('Captchas', WSB_Controller_Url::getUrl('index', 'captcha', null, 'test')) . '</li>');
            $this->_response->appendBody('<li>' . WSB_Controller_Url::getHtmlLink('Controller mit mehreren Models', WSB_Controller_Url::getUrl('index', 'list', null, 'test')) . '</li>');
            $this->_response->appendBody('<li>' . WSB_Controller_Url::getHtmlLink('Tabelle (schreiben/l&ouml;schen)', WSB_Controller_Url::getUrl('index', 'test-new-rows-of-all-tables', null, 'test')) . '</li>');
            $this->_response->appendBody('<li>' . WSB_Controller_Url::getHtmlLink('View - View helpers', WSB_Controller_Url::getUrl('index', 'view', null, 'test')) . '</li>');
            $this->_response->appendBody('</ul>');
            $this->_response->appendBody('</div>');
	}

        public function modelAction()
        {
            $timer = new Benchmark_Timer();
            $timer->start();

            $db = Zend_Db_Table_Abstract::getDefaultAdapter();
            $sql = 'SELECT * FROM feuser';
            $results = $db->fetchAll($sql);
            $timer->setMarker('Results');

            out(count($results));

            $mapper = new WSB_DbMapper();
            //$dbTable = $mapper->getDbTable();
            //out($dbTable);
            
            $collection = new WSB_Collection2($results);
            $collection->setModelClass('Test_Feuser2');
            foreach($collection AS $model) {
                $model->toArray(true);
            }

            $i = 0;
            foreach($results AS $row) {
                $model = new Test_Feuser1($row);
                $i++;
            }
            echo $i;
            $timer->setMarker('Model');

            $i = 0;
            foreach($results AS $row) {
                $model = new Test_Feuser2($row);
                $model->toArray();
                $i++;
            }
            echo $i;
            $timer->setMarker('Model2');

            $i = 0;
            $mapper = new WSB_DbMapper();
            foreach($results AS $row) {
                $model = new Test_Feuser3($row);
                $i++;
            }
            echo $i;
            $timer->setMarker('Model3');

            $timer->stop();
            $timer->display();


            $this->_helper->viewRenderer->setNoRender();

            $data = array(
                array(
                    'text1' => 'ABC',
                    'text2' => 'DEF',
                    'int1' => 12,
                    'int2' => 24
                ),
                array(
                    'text1' => 'abc',
                    'text2' => 'def',
                    'int1' => 1122,
                    'int2' => 2244
                ),
                array(
                    'text1' => 'XYZ',
                    'text2' => 'xyz',
                    'int1' => 88,
                    'int2' => 99
                )
            );
            $collection = new Test_Collection($data);
            $collection->setModelClass('Test_Model');
            foreach($collection AS $model) {
                echo $model->text1 . "<br />";
                echo $model->getText2() . "<br />";
                echo $model->getInt1() . "<br />";
                echo $model->int2 . "<br />";
            }


            $model = new Test_Model();
            $model->text1 = 'Hallo';
            $model->setText1('Hallo');
            $model->text2 = 'Thomas';
            $model->setText2('Thomas');
            $model->int1 = 2345;
            $model->setInt1(1234);
            $model->int2 = 1111;
            $model->setInt2(5678);

            out($model->getProperties());
            
        }

	public function indexAction()
	{

            $table = new Test_Model_DbTable_Language();
            $rowset = $table->fetchAll();
            $row = $table->fetchRow();
            //$testmodels = new Test_Model_LanguageSet(array());
            //$testmodel = new Test_Model_Language();
	}

	public function editorAction()
	{

		$this->_forward('form', 'editor', 'core', array('table' => 'beuser'));
		return;

		if($this->_request->isPost()) {

		}

		$this->_helper->viewRenderer->setNoRender();

		$table = new Member_Model_DbTable_Beuser();
		$row = $table->fetchRow('id=1');
		$form = $row->getForm();

		echo '<h2>Beuser</h2>';

		if($this->_request->isPost() && $form->isValid($_POST)) {
			out($form->getValues());
		} else {
			echo $form->render();
		}

		return;

		$table = new WSB_XT_Pio_Model_Table();
		$row = $table->fetchRow('id=119');
		$editor = new WSB_Form_Editor($row);
		$form = $editor->getForm();
		echo '<br /><h2>PIO Paper</h2>' . $form->render();

		$table = new Cms_Model_DbTable_Page();
		$row = $table->fetchRow('id=25');
		$editor = new WSB_Form_Editor($row);
		$form = $editor->getForm();
		echo '<br /><h2>Page</h2>' . $form->render();

		$table = new WSB_XT_News_Model_News_Table();
		$row = $table->fetchRow('id=10');
		$editor = new WSB_Form_Editor($row);
		$form = $editor->getForm();
		echo '<br /><h2>Newsitem</h2>' . $form->render();

	}

	public function paginatorAction()
	{
		$array = array();
		for($i=1; $i<=300; $i++) {
			$array[] = 'Testinhalt Nr. ' . $i;
		}
		$paginator = Zend_Paginator::factory($array);
		$paginator->setCurrentPageNumber($this->_getParam('page'));
		$this->view->paginator = $paginator;
	}

	public function captchaAction()
	{

		$this->_helper->viewRenderer->setNoRender();

		try {
			$this->_response->appendBody('<h1>Image</h1>');
			$captcha = new Zend_Captcha_Image(array(
			    'name' => 'foo',
			    'wordLen' => 5,
			    'timeout' => 300,
			    'font' => WSB_APP_PATH . 'res/arial.ttf',
			    'imgDir' => WSB_APP_PATH . 'tmp',
			    'imgUrl' => WSB_URL . 'WSB/tmp'
			));
			$id = $captcha->generate();
			$this->_response->appendBody( $captcha->render($this->view) );
		} catch (Exception $e) {
			$this->_response->appendBody( 'Ein Fehler ist beim Erzeugen des Captchaimages aufgetreten.');
		}

		$this->_response->appendBody('<h1>Figlet</h1>');
		$captcha = new Zend_Captcha_Figlet(array(
		    'name' => 'foo',
		    'wordLen' => 4,
		));
		$id = $captcha->generate();
		$this->_response->appendBody( $captcha->render($this->view) );

		$this->_response->appendBody('<h1>Dumb</h1>');
		$captcha = new Zend_Captcha_Dumb(array(
		    'name' => 'foo',
		    'wordLen' => 4,
		));
		$id = $captcha->generate();
		$this->_response->appendBody( $captcha->render($this->view) );

		return;

	}

	public function fileAction()
	{
		try {
			$dirIterator = new WSB_DirectoryIterator(WSB_PATH.'media/figures/');
			foreach($dirIterator AS $dir) {
				$fileinfo = $dir->getFileInfo();
				if($fileinfo->isFile()) {
					$fileinfo->printInfo();
				}
			}
		} catch (Exception $e) {
			echo 'Ausnahme gefangen: ',  $e->getMessage(), "\n";
		}

	}

	public function viewAction()
	{
		//out($this->view->getScriptPaths());
		//$this->_helper->viewRenderer->setNoRender();
		//$this->render();
	}

	public function quickformAction()
	{

		echo WSB_Icon::edit('test') . "<br />";
		//echo WSB_Icon::save('save') . "<br />";
		echo WSB_Icon::delete('delete') . "<br />";

	}

	public function dbAction()
	{

		$beuser = new WSB_Module_Test_Db_Beuser();
		$beuser = new WSB_Module_Test_Db_Beuser();

		$db = new WSB_Module_Test_Db_Language();
		foreach($db->fetchAll('deleted=0') AS $language) {
			out($language->toArray());
		}

		$language = new WSB_Module_Test_Model_Language();

		$db = new WSB_Module_Test_Db_Language();
		$set = array('iso' => 'AB', 'language' => 'AAABBBCCC', 'deleted' => 0);
		$language = $db->createRow($set);
		//$language->save();

	}

	public function testNewRowsOfAllTablesAction()
	{

		$this->_helper->viewRenderer->setNoRender();

		$this->_response->appendBody('<h1>Tabellen: Zeile schreiben und l&ouml;schen</h1>');
		$this->_response->appendBody('<p>In folgende Tabellen konnte erfolgreich eine Zeile geschrieben und anschliessend wieder gel&ouml;scht werden (NULL und NOT NULL wird ber&uuml;cksichtigt).</p>');
		$this->_response->appendBody('<p>');

		$delete = true;

		$table = new Member_Model_DbTable_Begroup();
		$row = $table->createRow();
		$row->name = 'name';
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('Member_Model_DbTable_Begroup');
		$this->_response->appendBody('<br />');

		$table = new Member_Model_DbTable_Bemember();
		$row = $table->createRow();
		$row->beuser_id = 9999;
		$row->begroup_id = 9999;
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('Member_Model_DbTable_Bemember');
		$this->_response->appendBody('<br />');

		$table = new Member_Model_DbTable_Beuser();
		$row = $table->createRow();
		$row->username = 'username';
		$row->password = 'password';
		$row->email = 'email';
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('Member_Model_DbTable_Beuser');
		$this->_response->appendBody('<br />');

		$table = new Cms_Model_DbTable_Container();
		$row = $table->createRow();
		$row->page_id = 99999;
		$row->language_id = 9999;
		$row->instance_id = 9999;
		$row->placeholder_id = 9999;
		$row->sort = 9999;
		$row->recursive = 0;
		$row->controller = 'Frontend';
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('Cms_Model_DbTable_Container');
		$this->_response->appendBody('<br />');

		$table = new Member_Model_DbTable_Fegroup();
		$row = $table->createRow();
		$row->name = 'name';
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('Member_Model_DbTable_Fegroup');
		$this->_response->appendBody('<br />');

		$table = new Member_Model_DbTable_Femember();
		$row = $table->createRow();
		$row->fegroup_id = 9999;
		$row->feuser_id = 9999;
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('Member_Model_DbTable_Femember');
		$this->_response->appendBody('<br />');

		$table = new Member_Model_DbTable_Feuser();
		$row = $table->createRow();
		$row->username = 'username';
		$row->password = 'password';
		$row->email = 'email';
		$row->lastname = 'lastname';
		$row->firstname = 'firstname';
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('Member_Model_DbTable_Feuser');
		$this->_response->appendBody('<br />');

		$table = new Cms_Model_DbTable_Language();
		$row = $table->createRow();
		$row->iso = 'XX';
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('Cms_Model_DbTable_Language');
		$this->_response->appendBody('<br />');

		$table = new System_Model_DbTable_Log();
		$row = $table->createRow();
		$row->beuser_id = 9999;
		$row->priority = 9999;
		$row->message = 'message';
		$row->ip = 'IP';
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('System_Model_DbTable_Log');
		$this->_response->appendBody('<br />');

		$table = new Cms_Model_DbTable_Xtension();
		$row = $table->createRow();
		$row->name = 'name';
		$row->class = 'class';
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('Cms_Model_DbTable_Xtension');
		$this->_response->appendBody('<br />');

		$table = new Cms_Model_DbTable_Page();
		$row = $table->createRow();
		$row->pid = 0;
		$row->sort = 9999;
		$row->nav_hide = 1;
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('Cms_Model_DbTable_Page');
		$this->_response->appendBody('<br />');

		$table = new Cms_Model_DbTable_PageOverlay();
		$row = $table->createRow();
		$row->page_id = 9999;
		$row->language_id = 9999;
		$row->title = 'title';
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('Cms_Model_DbTable_PageOverlay');
		$this->_response->appendBody('<br />');

		$table = new Cms_Model_DbTable_PageLanguage();
		$row = $table->createRow();
		$row->language_id = 9999;
		$row->page_id = 9999;
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('Cms_Model_DbTable_PageLanguage');
		$this->_response->appendBody('<br />');

		$table = new Cms_Model_DbTable_Placeholder();
		$row = $table->createRow();
		$row->name = 'name';
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('Cms_Model_DbTable_Placeholder');
		$this->_response->appendBody('<br />');

		$table = new Cms_Model_DbTable_Template();
		$row = $table->createRow();
		$row->pid = 0;
		$row->placeholder_id = 0;
		$row->nesting_mode = 0;
		$row->name = 'name';
		$row->html = 'html';
		$id = $row->save();
		if($delete) $table->delete('id='.$id);
		$this->_response->appendBody('Cms_Model_DbTable_Template');
		$this->_response->appendBody('<br />');

		$this->_response->appendBody('</p>');

	}

}
